#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import asyncio
import time
import aiohttp

async def get_com_details():
    yjj_file = open('./datas/药监局协程异步.text', mode='a', encoding='utf-8')
    url_id = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList'
    url_company = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById'
    async with aiohttp.ClientSession() as sess:
        for i in range(1, 355):
            data = {
                'on': 'true',
                'page': i,
                'pageSize': '15',
                'productName': '',
                'conditionType': '1',
                'applyname': '',
                'applysn': '',
            }
            try:
                async with await sess.post(url=url_id, data=data, timeout=1) as response:
                    try:
                        text_json = await response.json()
                    except Exception as e:
                        print(e)
                        text_json = await response.json(content_type='text/html',enconding='utf-8')
                    for list in text_json['list']:
                        # print(list['ID'])
                        com_data = {
                            'id': list['ID']
                        }
                        async with await sess.post(url=url_company,data=com_data,timeout=1) as response:
                            try:
                                com_text = await response.json()
                            except Exception as e:
                                print(e)
                                com_text = await response.json(content_type='text/html',enconding='utf-8')
                            com_text = str(com_text)
                            yjj_file.write(com_text+'\n')
            except Exception as e:
                print(e)
    yjj_file.close()

if __name__ == '__main__':
    start_time = time.time()
    tasks = []
    coroutine_tmep = get_com_details()
    task = asyncio.ensure_future(coroutine_tmep)
    loop = asyncio.get_event_loop()
    tasks.append(task)
    loop.run_until_complete(asyncio.wait(tasks))
    print('时长：',time.time()-start_time)